package modelimplementation

import (
	"salary/model"
	"time"
)

/*
 * 按销售情况支付
 */

type CommissionClassification struct {
	// 每笔销售单提成
	commissionRate float64
	// 底薪
	salary         float64
	// 销售凭条
	receipts       map[time.Time]SalesReceipt
}

func NewCommissionClassification(salary, rate float64) *CommissionClassification {
	return &CommissionClassification{commissionRate: rate, salary: salary}
}

func (cc *CommissionClassification) GetSalary() float64 {
	return cc.salary
}

func (cc *CommissionClassification) GetRate() float64 {
	return cc.commissionRate
}

func (cc *CommissionClassification) GetReceipt(date time.Time) SalesReceipt {
	return cc.receipts[date]
}

func (cc *CommissionClassification) AddSalesReceipt(salesReceipt SalesReceipt) {
	cc.receipts[salesReceipt.GetSaleDate()] = salesReceipt
}

func (cc *CommissionClassification) CalculatePay(pc model.PayCheck) float64 {
	commission := 0.0
	for date, salesReceipt := range cc.receipts {
		date1 := pc.GetPayPeriodStartDate()
		date2 := pc.GetPayPeriodEndDate()

		if date.After(date1) && date.Before(date2) {
			commission += salesReceipt.GetAmount() * cc.commissionRate
		}
	}
	return commission
}
